import React from 'react'
import { connect } from 'react-redux'
import Modal from './BookInfoModalUI'
import PubSub from 'pubsub-js'
import { GET_DETAIL } from '../constant'
import './index.less'

class BookInfoModal extends React.PureComponent {

  constructor (props) {
    super(props)
    this.state = {
      visible: false,

    }
  }

  componentDidMount () {
    //订阅的事件
    this.pubsub_token = []
    this.pubsub_token.push(PubSub.subscribe(GET_DETAIL, this.showModal))
  }

  showModal = () => {
    this.setState({
      visible: true
    })
  }
  onCancel = () => {
    this.setState({
      visible: false
    })
  }

  render () {
    const { onCancel } = this
    const { visible } = this.state
    const { bookInfo } = this.props
    return (
      <Modal
        visible={visible}
        onCancel={onCancel}
        bookInfo={bookInfo.toJS()}
      />
    )
  }
}

const mapStateToProps = (state) => {
  return {
    bookInfo: state.get('getBookInfo')
  }
}
export default connect(mapStateToProps)(BookInfoModal)